﻿//-----------------------------------------------------------------------
// <copyright file="Diff.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Mark Phillips</author>
// <email>v-mphill</email>
// <date>2004-05-27</date>
// <summary>Gets the difference betweek two times.
// </summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Time
{
    using System;
    using System.Globalization;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Returns the time difference between two times.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[
    /// <Time.Diff StartTime="hh:mm:ss" EndTime="hh:mm:ss">
    ///     <Output TaskParameter="Time" ItemName="ResultsItem" />
    /// </Time.Diff>]]></code>
    /// <para>where:</para>
    /// <para><i>StartTime (Required)</i></para>
    /// <para>Indicates the start time.</para>
    /// <para><i>EndTime (Output)</i></para>
    /// <para>Indicates the end time.</para>
    /// <para><i>Time (Output)</i></para>
    /// <para>Returns the difference between EndTime and StartTime.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Time.Diff StartTime="00:00:00" EndTime="00:01:00">
    ///             <Output TaskParameter="Time" ItemName="ResultsItem" />
    ///         </Time.Diff>
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class Diff : TaskBase
    {
        /// <summary>
        /// The difference between EndTime and StartTime.
        /// </summary>
        private string diffTime = String.Empty;

        /// <summary>
        /// The end time.
        /// </summary>
        private string endTime = String.Empty;

        /// <summary>
        /// The start time.
        /// </summary>
        private string startTime = String.Empty;

        /// <summary>
        /// Initializes a new instance of the Diff class.
        /// </summary>
        public Diff()
        {
        }

        /// <summary>
        /// Gets or sets the start time.
        /// </summary>
        [Required]
        public string StartTime
        {
            get
            {
                return (this.startTime == null ? String.Empty : this.startTime);
            }

            set
            {
                this.startTime = value;
            }
        }

        /// <summary>
        /// Gets or sets the end time.
        /// </summary>
        [Required]
        public string EndTime
        {
            get
            {
                return (this.endTime == null ? String.Empty : this.endTime);
            }

            set
            {
                this.endTime = value;
            }
        }

        /// <summary>
        /// Gets or sets the difference between EndTime and StartTime.
        /// </summary>
        [Output]
        public string Time
        {
            get
            {
                return (this.diffTime == null ? String.Empty : this.diffTime);
            }

            set
            {
                this.diffTime = value;
            }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code

            if (this.StartTime == String.Empty || this.StartTime.Length == 0)
            {
                throw new Exception("StartTime must be specified");
            }

            if (this.EndTime == String.Empty || this.EndTime.Length == 0)
            {
                throw new Exception("EndTime must be specified");
            }

            DateTime validStartTime = DateTime.Parse(this.startTime);
            DateTime validEndTime = DateTime.Parse(this.endTime);

            TimeSpan validDiffTime = validEndTime - validStartTime;

            this.diffTime = validDiffTime.ToString();

            #endregion
        }
    }
}
